package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.RelationPoet;
import com.example.springboot.entity.RelationType;
import com.example.springboot.mapper.RelationTypeMapper;
import com.example.springboot.service.RelationTypeService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class RelationTypeServiceImpl extends ServiceImpl<RelationTypeMapper, RelationType> implements RelationTypeService {
    @Override
    public List<RelationType> listRelationType(RelationType relationType) {
        LambdaQueryWrapper<RelationType> wrapper = Wrappers.lambdaQuery(relationType)
                .isNull(ObjectUtils.isEmpty(relationType.getRelationTypeId()), RelationType::getRelationTypeId);
        List<RelationType> list = this.list(wrapper);
        if (CollectionUtils.isNotEmpty(list)){
            list.forEach(info -> {
                RelationType queryRelationType = new RelationType();
                queryRelationType.setRelationTypeId(info.getId());
                List<RelationType> relationTypeList = listRelationType(queryRelationType);
                info.setRelationTypeList(relationTypeList);
            });
        }
        return list;
    }

    @Override
    public List<RelationType> countRelationType(RelationType relationType) {
        List<RelationType> list = listRelationType(relationType);
        if (CollectionUtils.isNotEmpty(list)){
            list.forEach(info -> {
                RelationType queryRelationType = new RelationType();
                queryRelationType.setRelationTypeId(info.getId());
                List<RelationType> relationTypeList = countRelationType(queryRelationType);
                long count = new RelationPoet().selectCount(Wrappers.lambdaQuery(RelationPoet.class)
                        .eq(RelationPoet::getRelationTypeId, info.getId()));
                info.setNumber(count);
                info.setRelationTypeList(relationTypeList);
            });
        }
        return list;
    }
}
